<?php

namespace app\admin\controller;

use think\Controller;
use app\index\model\Book;
use think\Db;
use think\Request;
// use think\Env;

class Shop extends Controller{

    // 展示商品列表
    public function shopList(Request $request){
        if(empty(session('username')))
        {
            $this->error('请先登录','login/login');
        }
        // $books =Db::table('book')->order('bookID','asc')->paginate(10);
        // $page=$books->render();
        // $this->assign('page',$page);        
        // $this->assign('books',$books);

//         $page = $request->param('apage') or 1;//当前的页码
        
        
        // 1、找出所有图书类别
        $bclass = Db::table('book')->distinct('ture')->field('bclass')->select();
        $this->assign('bclasses',$bclass);
        
        //2、筛选条件
        $minprice = $request->param('minprice');
        $minsell = $request->param('minsell');
        
        $bookID = $request->param('bookID');
        $bcls = $request->param('bclass');
        $searchstr = 'true ';
        if (!empty($bcls) && strcmp('商品类型', $bcls)){
            $searchstr.="and bclass='".$bcls."'";
        }
        if (!empty($bookID)){
            $searchstr.=" and bookID = '".$bookID."'";
        }
        if (!empty($minprice)){
            $searchstr.=" and yourprice >= '".$minprice."'";
        }
        if (!empty($minsell)){
            $searchstr.=" and SelledNum >= '".$minsell."'";
        }

        session("admin_bcls", $bcls);
        session("admin_bookID", $bookID);
        session("admin_minprice", $minprice);
        session("admin_minsell", $minsell);
        
        
        //3、通过条件分页查询
        $books = Db::table('book')->where($searchstr)->order('bookID','asc')->paginate(10);
        $page=$books->render();
        $this->assign('page',$page);
        $this->assign('books',$books);
        
//         //过滤---如果是ajax请求，直接返回请求的数据
//         $rootpath = Env::get('rootpath', '/bookTP');
//         if($request->isAjax()){
//             //将书本显示的内容组织起来
//             $html = "";
//             foreach($books->toArray()['data'] as $book){
//                 $str = <<<html
//                                 <tr>
//                                     <td>
//                                         <input type="checkbox" name="select" lay-skin="primary" value="{$book['bookID']}" class="chk"></td>
//                                     <td>{$book['bookID']}</td>
//                                     <td>{$book['bname']}</td>
//                                     <td>{$book['price']}</td>
//                                     <td>{$book['yourprice']}</td>
//                                     <td>{$book['SelledNum']}</td>
//                                     <td>{$book['storenum']}</td>
//                                     <td class="td-manage">
//                                         <a title="编辑" onclick="xadmin.open('编辑','{:url('shop/shopEdit')}?bookID={$book['bookID']}', 1200, 500)" href="javascript:;">
//                                             <i class="layui-icon">&#xe63c;</i></a>
//                                         <a title="删除" onclick="member_del(this,'{$book['bookID']}')" href="javascript:;">
//                                             <i class="layui-icon">&#xe640;</i></a>
//                                     </td>
//                                 </tr>
// html;
//                 $html .=  $str;
//             }
//             $result = ['html' => $html, 'page' => $page];
//             return json($result); //返回json格式的数据
//         }
        
        return $this->fetch();
    }
    

    //展示添加商品界面
    public function shopAdd(){
        return $this->fetch();
    }
    
    //删除商品
    public function shopDelete(){
        $book=Book::get(input('post.bookID'));
        $book->delete();
        $this->redirect('shop/shopList');
    }
    
    //批量删除
    public function DeleteMore(){
        $books=input('post.bookID/a');
        foreach($books as $book){
            Db::execute("delete from book where bookID=" . $book);
        }
        //$this->redirect('shop/shopList');
    }
    
    //展示编辑商品界面
    public function shopEdit(){
        $book = Book::get(input('get.bookID'));
        $this->assign('book',$book);
        $bclass = Db::table('book')->distinct('ture')->field('bclass')->select();
        $this->assign('bclasses',$bclass);
        return $this->fetch();
    }
    
    //编辑商品
    public function editBook(Request $request){
        $book = Book::get(input('post.bookID'));
        $book->bname=$request->param('bname');
        $book->bclass=$request->param('bclass');
        $book->shuoming=$request->param('shuoming');
        $book->price=$request->param('price');
        $book->yourprice=$request->param('yourprice');
        $book->storenum=$request->param('storenum');
        $book->tejia='否';
        $book->SelledNum=0;
        
        //上传文件picture
    $pictures=$request->file('pictures');
        if(!empty($pictures)){
            $info=$pictures->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
            $book->pictures=$info->getSaveName();
        }
    
        $picturem=$request->file('picturem');
        if(!empty($picturem)){
            $info=$picturem->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
            $book->picturem=$info->getSaveName();
        }
        $pictureb=$request->file('pictureb');
        if(!empty($pictureb)){
            $info=$pictureb->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
            $book->pictureb=$info->getSaveName();
        }
      
        $book->save();
        $this->success('编辑成功!','shop/shopReload');
        //return $this->redirect("index/index");
        
    }
    
    //添加书籍
    public function addBook(Request $request){
        $book = new Book();
        $book->bname=$request->param('bname');
        $book->bclass=$request->param('bclass');
        $book->shuoming=$request->param('shuoming');
        $book->price=$request->param('price');
        $book->yourprice=$request->param('yourprice');
        $book->storenum=$request->param('storenum');
        $book->tejia='否';
        $book->SelledNum=0;
        
        //上传文件pictures
        $pictures=$request->file('pictures');
        if	(empty($pictures)){
            $this->error('请选择上传文件');
        }
        $info=$pictures->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
        $book->pictures=$info->getSaveName();
        
        //上传文件picturem
        $picturem=$request->file('picturem');
        if	(empty($picturem)){
            $this->error('请选择上传文件');
        }
        $info=$picturem->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
        $book->picturem=$info->getSaveName();
        
        //上传文件pictureb
        $pictureb=$request->file('pictureb');
        if(!empty($pictureb)){
            $info=$pictureb->validate(['ext'=>'jpg,png'])->move(ROOT_PATH.'public/static'.DS.'image','');
            $book->pictureb=$info->getSaveName();
        }

        $book->save();
        $this->success('添加成功!','shop/shopAdd');
    }

    public function shopReload(){
        return $this->fetch();
    }
}